package com.wisdudu.blank.ui.user;

import android.content.Intent;
import android.databinding.ObservableField;
import android.text.TextUtils;

import com.kelin.mvvmlight.base.ViewModel;
import com.kelin.mvvmlight.command.ReplyCommand;
import com.wisdudu.blank.data.Injection;
import com.wisdudu.blank.data.repo.UserRepo;
import com.wisdudu.blank.data.source.remote.client.retrofit.subscribers.ProgressSubscriber;
import com.wisdudu.blank.data.source.remote.client.retrofit.subscribers.SubscriberOnNextErrorListener;
import com.wisdudu.blank.databinding.FragmentUserinfoSetBinding;
import com.wisdudu.blank.support.rxbus.RxBusFlag;
import com.wisdudu.blank.support.util.ToastUtil;
import com.wisdudu.blank.support.widget.loading.LoadingProgressDialog;
import com.wisdudu.blank.ui.common.SelectYMDActivity;

/**
 * Created by ypp on 2016/10/19.
 */
public class UserInfoSetVM implements ViewModel {
    public ObservableField<String> nickName = new ObservableField<>();
    public ObservableField<String> birthday = new ObservableField<>();
    private UserInfoSetFragment mFragment;
    private FragmentUserinfoSetBinding mBinding;
    public final ObservableField<Integer> etLength = new ObservableField<>();
    private UserRepo mUserRepo;
    private String uid;

    public UserInfoSetVM(UserInfoSetFragment mFragment, FragmentUserinfoSetBinding mBinding, String uid) {
        this.mFragment = mFragment;
        this.mBinding = mBinding;
        this.uid = uid;
        etLength.set(12);
        mUserRepo = Injection.provideUserRepo();
    }

    public ReplyCommand birthOnClick = new ReplyCommand(() -> {
        Intent intent = new Intent(mFragment.getActivity(), SelectYMDActivity.class);
        intent.putExtra(RxBusFlag.SELECT_YMD, birthday.get());
        intent.putExtra(RxBusFlag.SELECT_YMD_FLAG, 1);
        mFragment.startActivityForResult(intent, SelectYMDActivity.REQUEST_SELECT_BIRTHDAY);
    });


    public ReplyCommand confirmOnClick = new ReplyCommand(() -> {
        confirm();
    });

    private void confirm() {
        if (TextUtils.isEmpty(nickName.get().trim())) {
            ToastUtil.INSTANCE.toast("昵称不能为空");
            return;
        }
        //1男 2女
        int sex = mBinding.cbSex.isChecked() ? 1 : 2;

        if (TextUtils.isEmpty(birthday.get())) {
            ToastUtil.INSTANCE.toast("出生日期不能为空");
            return;
        }
        mUserRepo.editUserNick(Integer.parseInt(uid), nickName.get(), sex, 1)
                .subscribe(new ProgressSubscriber(new SubscriberOnNextErrorListener<Object>() {
                    @Override
                    public void onError(Throwable e, LoadingProgressDialog pd) {
                        ToastUtil.INSTANCE.toast(e.getMessage());
                    }

                    @Override
                    public void onNext(Object abs, LoadingProgressDialog pd) {
                        ToastUtil.INSTANCE.toast("操作成功");
                        mFragment.getActivity().finish();
                    }
                }, mFragment.getActivity(), "正在提交..."));
    }

}
